package firtest;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import utils.JDBCUtils;


public class ServlsetDemo2 extends HttpServlet {


    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    /**
     *  get 请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        doPost(req,resp);
    }

    /**
     * post 请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(  getServletConfig().getInitParameter("name")  );

        // 修改编码格式
//        req.setCharacterEncoding("UTF-8");		// 接受到的

        // resp.setContentType("text/html; charset=UTF-8");		// 修改发送的	text/html | text/plain | text/json

        // response.getWriter().append("一段文字 ");

        System.out.println(this);

        Connection connection = null;
        PreparedStatement pStatement = null;
        ResultSet rsSet = null;
        // 获取页面元素
        String nameString = req.getParameter("userName");
        System.out.println(nameString + " ccccc ");
        System.out.println(nameString);
        System.out.println("中文乱码");
        try {

            // 连接数据库
            connection = JDBCUtils.getConnection();

            // 创建sql语句
            String sql = "select * from student where userName = ?";
            // 与上sql问号一一对应

            pStatement = connection.prepareStatement(sql);
            pStatement.setString( 1, nameString );



            // 申请sql语句至数据库，返回结果至resultSet
            rsSet = pStatement.executeQuery();

            // 处理结果
            if(rsSet.next())
            {
                System.out.println(rsSet.next());
                resp.getWriter().append("true");
            }
            else {
                resp.getWriter().append("false");
            }

            resp.getWriter().flush();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                System.out.println("数据库查询失败");
                e.printStackTrace();
    //			 req.setAttribute("msg", "注册失败");
                req.getRequestDispatcher("/views/fails.jsp").forward(req, resp);
            } catch (NullPointerException e) {
                // TODO: handle exception
                System.out.println("信息不完整");
                req.setAttribute("msg", "注册失败,信息填写不完整");
                req.getRequestDispatcher("/views/fails.jsp").forward(req, resp);
            } finally {
                JDBCUtils.close(connection, pStatement);
            }
  }
}
